/*
 * @(#)HobakPlayRule.java $version 2013. 5. 15.
 *
 */

package com.pace.gonu.model.rule;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.util.CollectionUtils;

import com.pace.gonu.constants.HobakBoardXY;
import com.pace.gonu.constants.Order;
import com.pace.gonu.model.BoardPath;
import com.pace.gonu.model.PlayRock;
import com.pace.gonu.model.status.PlayStatus;

/**
 * @author pace
 */
public class HobakPlayRule implements PlayRule {

	private List<PointWithDestination> doMovePathList = new ArrayList<PointWithDestination>();

	public HobakPlayRule() {
		List<Integer> destination1 = new ArrayList<Integer>();
		destination1.add(2);
		PointWithDestination doMovePath1 = new PointWithDestination();
		doMovePath1.setPoints(1);
		doMovePath1.setDestinations(destination1);
		doMovePathList.add(doMovePath1);

		List<Integer> destination2 = new ArrayList<Integer>();
		destination2.addAll(Arrays.asList(new Integer[] {1, 3, 4}));
		PointWithDestination doMovePath2 = new PointWithDestination();
		doMovePath2.setPoints(2);
		doMovePath2.setDestinations(destination2);
		doMovePathList.add(doMovePath2);

		List<Integer> destination3 = new ArrayList<Integer>();
		destination3.add(2);
		PointWithDestination doMovePath3 = new PointWithDestination();
		doMovePath3.setPoints(3);
		doMovePath3.setDestinations(destination3);
		doMovePathList.add(doMovePath3);

		List<Integer> destination4 = new ArrayList<Integer>();
		destination4.addAll(Arrays.asList(new Integer[] {2, 5, 6, 7}));
		PointWithDestination doMovePath4 = new PointWithDestination();
		doMovePath4.setPoints(4);
		doMovePath4.setDestinations(destination4);
		doMovePathList.add(doMovePath4);


		List<Integer> destination5 = new ArrayList<Integer>();
		destination5.addAll(Arrays.asList(new Integer[] {4, 6, 8}));
		PointWithDestination doMovePath5 = new PointWithDestination();
		doMovePath5.setPoints(5);
		doMovePath5.setDestinations(destination5);
		doMovePathList.add(doMovePath5);

		List<Integer> destination6 = new ArrayList<Integer>();
		destination6.addAll(Arrays.asList(new Integer[] {4, 5, 7, 8}));
		PointWithDestination doMovePath6 = new PointWithDestination();
		doMovePath6.setPoints(6);
		doMovePath6.setDestinations(destination6);
		doMovePathList.add(doMovePath6);

		List<Integer> destination7 = new ArrayList<Integer>();
		destination7.addAll(Arrays.asList(new Integer[] {4, 6, 8}));
		PointWithDestination doMovePath7 = new PointWithDestination();
		doMovePath7.setPoints(7);
		doMovePath7.setDestinations(destination7);
		doMovePathList.add(doMovePath7);

		List<Integer> destination8 = new ArrayList<Integer>();
		destination8.addAll(Arrays.asList(new Integer[] {5, 6, 7, 10}));
		PointWithDestination doMovePath8 = new PointWithDestination();
		doMovePath8.setPoints(8);
		doMovePath8.setDestinations(destination8);
		doMovePathList.add(doMovePath8);

		List<Integer> destination9 = new ArrayList<Integer>();
		destination9.add(10);
		PointWithDestination doMovePath9 = new PointWithDestination();
		doMovePath9.setPoints(10);
		doMovePath9.setDestinations(destination9);
		doMovePathList.add(doMovePath9);

		List<Integer> destination10 = new ArrayList<Integer>();
		destination10.addAll(Arrays.asList(new Integer[] {8, 9, 11}));
		PointWithDestination doMovePath10 = new PointWithDestination();
		doMovePath10.setPoints(10);
		doMovePath10.setDestinations(destination10);
		doMovePathList.add(doMovePath10);

		List<Integer> destination11 = new ArrayList<Integer>();
		destination11.add(10);
		PointWithDestination doMovePath11 = new PointWithDestination();
		doMovePath11.setPoints(11);
		doMovePath11.setDestinations(destination11);
		doMovePathList.add(doMovePath11);

	}

	@Override
	public List<BoardPath> getMovePath(List<PlayRock> playRocks, PlayRock moveRock) {
		List<BoardPath> boardPaths = new ArrayList<BoardPath>();
		
		int boardPathIndex = HobakBoardXY.getPoint(moveRock);
		List<Integer> destinationList = doMovePathList.get(boardPathIndex).getDestinations();
		for (Integer destination : destinationList) {
			if (moveRock.getInitPathIndex() == destination) {
				continue;
			}

			BoardPath boardPath = new BoardPath();
			HobakBoardXY boardXY = HobakBoardXY.values()[destination - 1];

			if (!isPlayRockPath(playRocks, boardXY)) {
				boardPath.setX(boardXY.getX());
				boardPath.setY(boardXY.getY());
				boardPaths.add(boardPath);
			}
		}
		
		return boardPaths;
	}

	private boolean isPlayRockPath(List<PlayRock> playRocks, HobakBoardXY boardXY) {
		for (PlayRock playRock : playRocks) {
			if (playRock.getPath().getX() == boardXY.getX() && playRock.getPath().getY() == boardXY.getY()) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean isEndGame(PlayStatus playStatus, Order order) {
		List<PlayRock> playRocks = playStatus.getPlayRocks();
		for (PlayRock playRock : playRocks) {
			if (playRock.getOrder().equals(order.toString())) {
				if (!CollectionUtils.isEmpty(this.getMovePath(playRocks, playRock))) {
					return false;
				}
			}
		}
		return true;
	}

	@Override
	public boolean isCharm(List<PlayRock> playRocks, PlayRock playRock) {
		return false;
	}

	@Override
	public boolean isMove(PlayStatus playStatus) {
		return false;
	}

}
